<?php
/**
 * This file is form http://findcat.cn
 *
 * @link     https://findcat.cn
 * @email    1476982312@qq.com
 */
namespace App\Http\Controllers\Home;

use App\Models\Tag;
use App\Models\Article;
use App\Models\Category;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CategoryController extends Controller
{
    /**
     * 获取当前分类的文章.
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show(Request $request)
    {
        $page = $request->page;

        $articles = Article::query()
            ->where('status', true)
            ->where('categories_id', $request->category_id)
            ->orderBy('is_top', 'asc')
            ->orderByDesc('created_at')
            ->paginate();

        $title = Category::query()
            ->where('id', $request->category_id)
            ->first();

        $articles->page = $page;

        $tags = Tag::query()->limit(5)->get();

        $data = [
            'title'     => $title->name,
            'articles'  => $articles,
            'tags'      => $tags,
        ];

        return view('home.index', $data);
    }
}
